<?php

namespace App\Http\Controllers\v1\activity;

use App\Http\Controllers\Controller;
use App\services\v1\DrawServices;
use App\services\v1\LuckServices;
use Dingo\Api\Http\Request;
use Illuminate\Http\JsonResponse;
use Illuminate\Validation\ValidationException;

class DrawController extends Controller
{
    public function rules()
    {
        return [
            'name' => 'required',
            'url'  => 'nullable|url',
            'probability' => 'required|integer',
            'activityId'  => 'required|integer',
        ];
    }
    public function attributes()
    {
        return [
            'name' => '奖品名称',
            'url'  => '奖品URL',
            'probability' => '奖品中奖概率',
            'activityId'  => '奖品所属活动ID',
        ];
    }

    /**
     * 增加奖品
     * @param Request $request
     * @return false|JsonResponse|string
     * @throws ValidationException
     */
    public function addDraw(Request $request)
    {
        $params     = $this->validate($request, $this->rules(),[],$this->attributes());
        $addDrawRes = (new DrawServices())->addDraw($params);
        if ($addDrawRes['code'] != 200) {
            return $this->error($addDrawRes['code']);
        }
        return $this->success();
    }

    /**
     * 奖品列表
     * @param Request $request
     * @return false|string
     * @throws ValidationException
     */
    public function drawList(Request $request)
    {
        $params = $this->validate($request, [
            'acId' => 'required|integer',
        ], [], [
            'acId' => '活动ID',
        ]);
        $activityId = $params['acId'];
        $drawList = (new DrawServices())->getDrawList($activityId);
        return $this->success($drawList['data']);
    }
}